package cn.itheima.controller.module;

import cn.itheima.controller.base.BaseComtroller;
import cn.itheima.domain.Module;
import cn.itheima.service.module.ModuleService;
import cn.itheima.util.UtilFuns;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;
@SuppressWarnings("all")
@Controller
@RequestMapping(value = "/system/module")
public class ModuleController extends BaseComtroller {
    @Autowired
    private ModuleService moduleService;

    @RequestMapping(name = "查询模块",value = "/list")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize){

        PageInfo pb = moduleService.findAllPage(pageNum,pageSize);
        request.setAttribute("pb",pb);

        return "/system/module/module-list";
    }
    @RequestMapping(name = "删除模块",value = "/delete")
    public String delete(String id){
        moduleService.delete(id);

        return "redirect:/system/module/list.do";
    }

    @RequestMapping(name = "查询回显模块",value = "/toAdd")
    public String toAdd(){
       List<Module> list= moduleService.findAll();
        request.setAttribute("menus",list);
        return "/system/module/module-add";
    }
    @RequestMapping(name = "添加模块",value = "/edit")
    public String edit(Module module){
        if (UtilFuns.isEmpty(module.getId())){
            String s = UUID.randomUUID().toString();
            module.setId(s);
            moduleService.edit(module);
        }else {
            moduleService.update(module);
        }
        return "redirect:/system/module/list.do";
    }
    @RequestMapping(name = "添加模块",value = "/toUpdate")
    public String edit(String id){
        Module module=moduleService.findById(id);
        request.setAttribute("module",module);
        return "/system/module/module-add";
    }

}
